<?php

namespace App\Http\Controllers\Api;

use App\Common\Constant\Status;
use App\Events\CouponAssign;
use App\Events\MasterAssureFund;
use App\Events\MasterOrderRefund;
use App\Events\MerchantOrderArbitrate;
use App\Events\MerchantOrderComplain;
use App\Events\MerchantOrderRefund;
use App\Events\MerchantRecharge;
use App\Events\OrderAppointed;
use App\Events\OrderDelivered;
use App\Events\OrderEmployed;
use App\Events\OrderEvaluated;
use App\Events\OrderFinished;
use App\Events\OrderOffered;
use App\Events\OrderSuccess;
use App\Events\Tested;
use App\Models\EventLog;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 事件触发
 * Class EventController
 * @package App\Http\Controllers\Api
 */
class EventController extends BaseController
{

    /**
     * 测试
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function tested($order_id = null,Request $request)
    {
        return $this->renderJson(event(new Tested($order_id,$request->input('ip'))) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 师傅对订单报价
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function orderOffered($order_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderOffered::class,
            'params'=>['order_id'=>$order_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderOffered($order_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 商家已支付预付款雇佣师傅
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function orderEmployed($trade_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderEmployed::class,
            'params'=>['trade_id'=>$trade_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderEmployed($trade_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 师傅已预约客户
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function orderAppointed($order_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderAppointed::class,
            'params'=>['order_id'=>$order_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderAppointed($order_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 师傅已操作物流提货
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function orderDelivered($order_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderDelivered::class,
            'params'=>['order_id'=>$order_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderDelivered($order_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 师傅已操作订单确认完成服务
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function orderFinished($order_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderFinished::class,
            'params'=>['order_id'=>$order_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderFinished($order_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 订单完成商家已确认放款，或超过时限未确认系统自动放款
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function orderSuccess($order_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderSuccess::class,
            'params'=>['order_id'=>$order_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderSuccess($order_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 师傅操作商家退款请求
     * @param Order $order
     * @param Request $request
     * @return string
     */
    public function masterOrderRefund($refund_id,Request $request)
    {
        EventLog::create([
            'event'=>MasterOrderRefund::class,
            'params'=>['refund_id'=>$refund_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new MasterOrderRefund($refund_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 商家给订单/师傅评分
     * @param $master_id
     * @param Request $request
     */
    public function orderEvaluated($evaluate_id,Request $request)
    {
        EventLog::create([
            'event'=>OrderEvaluated::class,
            'params'=>['evaluate_id'=>$evaluate_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new OrderEvaluated($evaluate_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 商家发起退款
     * @param $master_id
     * @param Request $request
     */
    public function merchantOrderRefund($refund_id,Request $request)
    {
        EventLog::create([
            'event'=>MerchantOrderRefund::class,
            'params'=>['refund_id'=>$refund_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new MerchantOrderRefund($refund_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 商家申请仲裁
     * @param $master_id
     * @param Request $request
     */
    public function merchantOrderArbitrate($refund_id,Request $request)
    {
        EventLog::create([
            'event'=>MerchantOrderArbitrate::class,
            'params'=>['refund_id'=>$refund_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new MerchantOrderArbitrate($refund_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 商家发起投诉
     * @param $master_id
     * @param Request $request
     */
    public function merchantOrderComplain($complain_id,Request $request)
    {
        EventLog::create([
            'event'=>MerchantOrderComplain::class,
            'params'=>['complain_id'=>$complain_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new MerchantOrderComplain($complain_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 师傅缴纳质保金
     * @param $master_id
     * @param Request $request
     */
    public function masterAssureFund($fund_id,Request $request)
    {
        EventLog::create([
            'event'=>MasterAssureFund::class,
            'params'=>['fund_id'=>$fund_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new MasterAssureFund($fund_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 商家充值
     * @param $master_id
     * @param Request $request
     */
    public function merchantRecharge($recharge_id,Request $request)
    {
        EventLog::create([
            'event'=>MerchantRecharge::class,
            'params'=>['recharge_id'=>$recharge_id],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new MerchantRecharge($recharge_id)) ? Status::SUCCESS : Status::FAIL);
    }

    /**
     * 发放优惠券
     * @param $master_id
     * @param Request $request
     */
    public function couponAssign($merchant_id,$coupon_id,$count,Request $request)
    {
        EventLog::create([
            'event'=>CouponAssign::class,
            'params'=>['merchant_id'=>$merchant_id,'coupon_id'=>$coupon_id,'count'=>$count],
            'request_params'=>$request->all(),
            'ip'=>$request->input('ip'),
        ]);
        return $this->renderJson(event(new CouponAssign($merchant_id,$coupon_id,$count)) ? Status::SUCCESS : Status::FAIL);
    }

}
